package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.PDealerDto;
import com.ruoyi.system.domain.vo.PDealerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.PDealer;
import com.ruoyi.system.service.IPDealerService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 经销商Controller
 * 
 * @author ruoyi
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/dealer")
public class PDealerController extends BaseController
{
    @Autowired
    private IPDealerService pDealerService;

    /**
     * 查询经销商列表
     */
    @RequiresPermissions("system:dealer:list")
    @GetMapping("/list")
    public TableDataInfo list(PDealer pDealer)
    {
        startPage();
        List<PDealerVo> list = pDealerService.selectPDealerList(pDealer);
        return getDataTable(list);
    }

    /**
     * 导出经销商列表
     */
    @RequiresPermissions("system:dealer:export")
    @Log(title = "经销商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PDealer pDealer)
    {
        List<PDealerVo> list = pDealerService.selectPDealerList(pDealer);
        ExcelUtil<PDealerVo> util = new ExcelUtil<>(PDealerVo.class);
        util.exportExcel(response, list, "经销商数据");
    }

    /**
     * 获取经销商详细信息
     */
    @RequiresPermissions("system:dealer:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pDealerService.selectPDealerById(id));
    }

    /**
     * 新增经销商
     */
//    @RequiresPermissions("system:dealer:add")
    @Log(title = "经销商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PDealerDto pDealer)
    {
        try {

            return toAjax(pDealerService.insertPDealer(pDealer));
        } catch (Exception e){
            e.printStackTrace();
            return   AjaxResult.error(e.getMessage());
        }
        }


    /**
     * 修改经销商
     */
    @RequiresPermissions("system:dealer:edit")
    @Log(title = "经销商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PDealerDto pDealer) throws Exception {
        return toAjax(pDealerService.updatePDealer(pDealer));
    }

    /**
     * 删除经销商
     */
    @RequiresPermissions("system:dealer:remove")
    @Log(title = "经销商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pDealerService.deletePDealerByIds(ids));
    }
}
